<?php
namespace app\admin\controller;

use app\admin\model\ArticleModel;
use Qiniu\Auth;
use think\Db;
use think\Validate;

class Article extends Common
{

    public function article_category()
    {
      $list = Db::name('article_type')->field('name,id')->order('id','desc')
          ->paginate(10,[]);
      return view('',['list'=>$list]);
    }

    public function article_list()
    {
        $type = input('param.type_id',1);
        $list = Db::name('article')
            ->field('title,is_top,is_show,id,create_time')->order('id','desc')
            ->where('type_id',$type)
            ->paginate(10,[]);
        $type_list = Db::name('article_type')->cache('type_list',86400)->field('name,id')->order('id','asc')->select();
        return view('',['list'=>$list,'type_list'=>$type_list]);
    }
    public function edit_article(){

        if(Request()->isPost()) {
            $id = input('param.id',0);
            if(!$id){
                return package(20001,'id错误');
            }
            $param = input('param.');
            $rule = [
                'type_id|类型'=>'require|number',
                'title|文章标题' =>'require|max:60',
                'content|文章内容' =>'require|max:4000'
            ];
            $validate = Validate::make($rule);
            if(isset($param['is_show']) && $param['is_show'] =='on'){
                $param['is_show']  = 1;
            }else{
                $param['is_show']  = 0;
            }
            $param['is_top'] =  isset($param['is_top'])?intval($param['is_top']):0;

            if($param['is_top'] ==='on'){
                $param['is_top']  = 1;
            }
            if (!$validate->check($param)) {
                return package(10002,$validate->getError());
            }
            $m = new ArticleModel();
            return checkRes($m->allowField(true)->save($param,['id'=>$id]),'修改');
        }else{
            $id = input('param.id', 0);
            $type_list = Db::name('article_type')->cache('type_list',86400)->field('name,id')->order('id','asc')->select();
            return view('', [
                'info' => Db::name('article')
                    ->where('id', $id)->find(),
                'type' => "edit_article",
                'type_list'=>$type_list,
            ]);
        }
    }
    public function add_article(){
        if(Request()->isPost()) {
            $param = input('param.');
            $rule = [
                'type_id|类型'=>'require|number',
                'title|文章标题' =>'require|max:60',
                'content|文章内容' =>'require|max:4000'
            ];
            $validate = Validate::make($rule);
            if(isset($param['is_show']) && $param['is_show'] =='on'){
                $param['is_show']  = 1;
            }else{
                $param['is_show']  = 0;
            }
            $param['is_top'] =   isset($param['is_top'])?intval($param['is_top']):0;
            if($param['is_top'] ==='on'){
                $param['is_top']  = 1;
            }
            if (!$validate->check($param)) {
                return package(10002,$validate->getError());
            }
            $param['create_time'] = date("Y-m-d H:i:s");
            $m = new ArticleModel();
            return checkRes($m->allowField(true)->save($param),'添加');
        }else{
            $type_list = Db::name('article_type')->cache('type_list',86400)->field('name,id')->order('id','desc')->select();
            return view('edit_article', ['type_list'=>$type_list,
                'type' => "edit_article"
            ]);
        }
    }
}